using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;

namespace DAL
{
    public class tbCTHoaDonDAL
    {
        private SqlConnection cn = null;
        public tbCTHoaDonDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbCTHoaDonDAL(tbCTHoaDonDAL tan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbCTHoaDon th)
        {
            SqlCommand cm = new SqlCommand("tbCTHoaDon_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mahd", th.mahd);
            cm.Parameters.AddWithValue("@sanpham", th.sanpham);
            cm.Parameters.AddWithValue("@soluong", th.sanpham);
            cm.Parameters.AddWithValue("@dongia", th.dongia);
            cm.ExecuteNonQuery();
        }
        public void Update(tbCTHoaDon th)
        {
            SqlCommand cm = new SqlCommand("tbCTHoaDon_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mahd", th.mahd);
            cm.Parameters.AddWithValue("@sanpham", th.sanpham);
            cm.Parameters.AddWithValue("@soluong", th.sanpham);
            cm.Parameters.AddWithValue("@dongia", th.dongia);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbCTHoaDon_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mahd", iD);
            cm.ExecuteNonQuery();
        }
        public tbCTHoaDonCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbCTHoaDon_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTHoaDonCollection cthdCollection = new tbCTHoaDonCollection();

            while (dr.Read())
            {
                tbCTHoaDon th = new tbCTHoaDon();
                th.mahd = int.Parse(dr.GetValue(0).ToString());
                th.sanpham = int.Parse(dr.GetValue(1).ToString());
                th.SoLuong = int.Parse(dr.GetValue(2).ToString());
                th.dongia = int.Parse(dr.GetValue(3).ToString());
                cthdCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return cthdCollection;
        }
    }
}
